//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.dao.wills;

import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.OrderSet;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.BaseDao;
import com.aidong.model.wills.ContractInfo;
import com.aidong.model.wills.MemberCard;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.hibernate.SQLQuery;
import org.springframework.stereotype.Repository;

@Repository
public class MemberCardDao extends BaseDao<MemberCard, String> {
	public MemberCardDao() {
	}

	public MemberCard getById(String cardCode) {
		return this.getByCardCode(cardCode);
	}

	public MemberCard getByCardCode(String cardCode) {
		return (MemberCard)this.getByWhere("cardCode=:p1", new Object[]{cardCode});
	}

	public boolean chkCardExists(String code1, String code2) {
		String sql = "select count(*) from MemberCard where cardCode>=:p1 and cardCode<=:p2";
		return ((Integer)this.getBySql(Integer.class, sql, new Object[]{code1, code2})).intValue() > 0;
	}

	public List<MemberCard> getNotUseList(String shopCode) {
		return this.queryList("shopCode=:p1 and status=1", new Object[]{shopCode}, (new OrderSet()).asc("cardCode"), -1);
	}

	public List<MemberCard> getNotUseTypeCount(String shopCode) {
		String sql = "select left(cardCode,1) cardType,count(*) status from MemberCard\nwhere shopCode=:p1 and status=1 group by left(cardCode,1) order by cardType";
		return this.queryListBySql(MemberCard.class, sql, new Object[]{shopCode});
	}

	public void queryPageSaleList(Pagination<ContractInfo> pg, ConditionMap cd) {
		ArrayList params = new ArrayList();
		String sqlw = cd.toSql(params);
		String sql = "select c.cardCode, c.shopCode, c.memberCode, c.contractCode, c.status, k.saleDate, k.beginTime, k.endTime, k.cardType,\ni.chName memberName, i.gender, i.idType, i.idNumber, i.memberPhoto, c.makeCardDate, c.shopReceiveDate, c.memberReceiveDate\nfrom MemberCard c inner join MemberContract k on c.contractCode=k.contractCode\ninner join MemberInfo i on c.memberCode=i.memberCode\n" + (sqlw.length() > 0?"where " + sqlw:"") + " order by c.useDate desc, k.saleDate desc";
		this.queryPageListBySql(ContractInfo.class, pg, sql, params.toArray());
	}

	public void addCardList(List<String> cardList, String cardType, String shopCode, String userCode) {
		String sql = "insert into MemberCard (CardCode, CardType, ShopCode, Status, StopMake, ReceiveContract, CreateUser, CreateTime, UpdateTime) values (:p1, :p2, :p3, 1, 0, 0, :p4, getdate(), getdate())";
		SQLQuery query = this.getSession().createSQLQuery(sql);
		query.setString("p2", cardType);
		query.setString("p3", shopCode);
		query.setString("p4", userCode);
		Iterator i$ = cardList.iterator();

		while(i$.hasNext()) {
			String cardCode = (String)i$.next();
			query.setString("p1", cardCode);
			query.executeUpdate();
		}

	}

	public void delCardList(List<String> cardList) {
		String sql = "delete from MemberCard where CardCode = :p1 and status=1";
		SQLQuery query = this.getSession().createSQLQuery(sql);
		Iterator i$ = cardList.iterator();

		while(i$.hasNext()) {
			String cardCode = (String)i$.next();
			query.setString("p1", cardCode);
			query.executeUpdate();
		}

	}

	public void updateMakeCard(MemberCard card) {
		String sql = "update MemberCard set status=:p1, makeCardUser=:p2, makeCardDate=:p3 where cardCode=:p4";
		this.updateBySql(sql, new Object[]{card.getStatus(), card.getMakeCardUser(), card.getMakeCardDate(), card.getCardCode()});
	}

	public void updateShopReceive(MemberCard card) {
		String sql = "update MemberCard set status=:p1, shopReceiveUser=:p2, shopReceiveDate=:p3 where cardCode=:p4";
		this.updateBySql(sql, new Object[]{card.getStatus(), card.getShopReceiveUser(), card.getShopReceiveDate(), card.getCardCode()});
	}

	public void updateMemberReceive(MemberCard card) {
		String sql = "update MemberCard set status=:p1, memberReceiveUser=:p2, memberReceiveDate=:p3 where cardCode=:p4";
		this.updateBySql(sql, new Object[]{card.getStatus(), card.getMemberReceiveUser(), card.getMemberReceiveDate(), card.getCardCode()});
	}
}
